﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BPM.Base.Tools
{
   public static  class MyDebug
    {
        static System.Diagnostics.TextWriterTraceListener listener;
        [System.Diagnostics.Conditional("DEBUG")]
        public static void addDebugListener(string filePathName)
        {


            if (listener == null)
            {
                //filePathName //路径格式与是否可操作判断
                listener = new System.Diagnostics.TextWriterTraceListener(filePathName);
            }
            if (!System.Diagnostics.Debug.Listeners.Contains(listener))
            {
                System.Diagnostics.Debug.Listeners.Add(listener);
            }


        }

        [System.Diagnostics.Conditional("DEBUG")]
        public static void removeDebugListener()
        {

            if (System.Diagnostics.Debug.Listeners.Contains(listener))
            {
                System.Diagnostics.Debug.Listeners.Remove(listener);

                listener.Flush();

                listener.Close();

                listener.Dispose();

                listener = null;
            }


        }

        [System.Diagnostics.Conditional("DEBUG")]
        public static void assert(bool condition, string message)
        {

            if (listener == null)
            {
                System.Diagnostics.Debug.Assert(condition, message);

            }
            else
            {
                System.Diagnostics.Debug.WriteLine(message);
                listener.Flush();
            }
        }

        [System.Diagnostics.Conditional("DEBUG")]
        public static void write(string message)
        {
            if (listener == null)
            {
                string filePathName = MyConfig.getAppValue("debugFile");
                if (!string.IsNullOrEmpty(filePathName))
                {
                    listener = new System.Diagnostics.TextWriterTraceListener(filePathName);

                    if (!System.Diagnostics.Debug.Listeners.Contains(listener))
                    {
                        System.Diagnostics.Debug.Listeners.Add(listener);
                    }
                }
            }


           System.Diagnostics.Debug.WriteLine(message + " : " + System.DateTime.Now.ToString());
           if (listener != null)
           {
               listener.Flush();
    
           }
        }

        [System.Diagnostics.Conditional("DEBUG")]
        public static void close()
        {
            if (listener != null)
            {
                listener.Flush();
                listener.Close();
            }

        }

    }
}
